package com.example.demo.command;

import ch.selise.exercise.pattern.notepad.command.CommandExecutor;
import ch.selise.exercise.pattern.notepad.command.GetCommandParameter;
import ch.selise.exercise.pattern.notepad.exception.EmptyStackException;
import ch.selise.exercise.pattern.notepad.exception.ExecuteException;
import ch.selise.exercise.pattern.notepad.exception.UnsupportedCommandException;

import com.vaadin.data.Item;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;

public class DeleteActionCommand extends ActionCommand implements ClickListener {

	
	private static final long serialVersionUID = 672001619090715939L;
	
	public DeleteActionCommand(String command, Table table) {
		super(command,table);
	}

	@Override
	protected void postClickAction(ClickEvent event) {
		
		if(event.getButton().getData() != null){
			
			Integer rowId = (Integer) event.getButton().getData();
			Item rowItem = getTableX().getItem(rowId);
			
			String text = rowItem.getItemProperty("Text").getValue()
					.toString();
			System.out.println("Row Id : " + rowId);
			System.out.println("Text : " + text);
			
			CommandExecutor commandExecutor = CommandExecutor.getInstance();;
			try {
				commandExecutor.executeCommand(new GetCommandParameter(getCommand(), text, rowId));
			}  catch (ExecuteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (EmptyStackException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (UnsupportedCommandException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			finally{
				reloadTable();
			}
		}
	}
	
}
